package net.entelijan.cobean.examples.showcase.bind.table;

import static org.junit.Assert.assertEquals;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JTableFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Test;

public class MasterDetailTableFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory
			.getLog(MasterDetailTableFestTestCase.class);

	@Test
	public void testInitialization() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			String[][] expectedContent = new String[][] {
					{ "Ad ora incerta", "1984", "Primo Levi" },
					{ "Se non ora, quando ?", "1984", "Primo Levi" },
					{ "La chiave a stella", "1978", "Primo Levi" },
					{ "A Bend in the River", "1979", "V.S. Naipaul" },
					{ "A House for Mr Bivas", "1961", "V.S. Naipaul" },
					{ "Miguel Street", "1959", "V.S. Naipaul" },
					{ "The Suffrage of Elvira", "1958", "V.S. Naipaul" },
					{ "The Mystic Masseur", "1957", "V.S. Naipaul" },
					{ "The Scream", "2008", "Rohinton Mistry" },
					{ "Family Matters", "2002", "Rohinton Mistry" },
					{ "Such a Long Journey", "1991", "Rohinton Mistry" },
					{ "A Fine Balance", "1996", "Rohinton Mistry" },
					{ "The Cost of Living", "1999", "Arunhati Roy" },
					{ "The End of Imagination", "1989", "Arunhati Roy" },
					{ "The Algebra of Infinite Justice", "2002", "Arunhati Roy" },
					{ "The God of Small Things", "1997", "Arunhati Roy" },
					{ "The Humble Administrator's Garden", "1985",
							"Vikram Seth" },
					{ "Mappings", "1980", "Vikram Seth" },
					{ "An Equal Music", "1999", "Vikram Seth" },
					{ "A Suitable Boy", "1993", "Vikram Seth" },
					{ "The Golden Gate", "1986", "Vikram Seth" },
					{ "The Enchantress of Florence", "2008", "Salman Rushdie" },
					{ "The Moor's Last Sight", "1995", "Salman Rushdie" },
					{ "Imaginary Homelands", "1992", "Salman Rushdie" },
					{ "Haroun and the Sea of Stories", "1990", "Salman Rushdie" },
					{ "The Satanic Verses", "1988", "Salman Rushdie" },
					{ "The Jaguar Smile", "1987", "Salman Rushdie" },
					{ "Shame", "1983", "Salman Rushdie" },
					{ "Midnight's Children", "1981", "Salman Rushdie" },
					{ "Grimus", "1975", "Salman Rushdie" } };

			final JTableFixture tableFix = frameFix.table("table");
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// Check the initial content of the table.
			tableFix.requireContents(expectedContent);

			// Check the selection
			String selectionValue = tableFix.selectionValue();
			assertEquals("A House for Mr Bivas", selectionValue);

			// Check the detail values
			titleTextBox.requireText("A House for Mr Bivas");
			yearTextBox.requireText("1961");
			authorTextBox.requireText("V.S. Naipaul");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromGUI() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			final JTableFixture tableFix = frameFix.table("table");
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// select one book
			{
				tableFix.selectCell(tableFix.cell("The Cost of Living"));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// select another book
			{
				tableFix.selectCell(tableFix.cell("Family Matters"));
				pause(200);

				titleTextBox.requireText("Family Matters");
				yearTextBox.requireText("2002");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Family Matters", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(2002, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// edit selected title
			{
				titleTextBox.setText("Adam Eva");
				yearTextBox.focus();
				pause(200);

				yearTextBox.requireText("2002");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(2002, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// edit selected year
			{
				yearTextBox.setText("1000");
				titleTextBox.focus();
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// reselect the first book
			{
				tableFix.selectCell(tableFix.cell("The Cost of Living"));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// reselect the edited book
			{
				tableFix.selectCell(tableFix.cell("Adam Eva"));
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromGUIWithSorting() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			final JTableFixture tableFix = frameFix.table("table");
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// select by title
			{
				tableFix.tableHeader().clickColumn(0);
				pause(200);
			}

			// select one book
			{
				tableFix.selectCell(tableFix.cell("The Cost of Living"));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// select another book
			{
				tableFix.selectCell(tableFix.cell("Family Matters"));
				pause(200);

				titleTextBox.requireText("Family Matters");
				yearTextBox.requireText("2002");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Family Matters", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(2002, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// edit selected title
			{
				titleTextBox.setText("Adam Eva");
				yearTextBox.focus();
				pause(200);

				yearTextBox.requireText("2002");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(2002, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// edit selected year
			{
				yearTextBox.setText("1000");
				titleTextBox.focus();
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// reselect the first book
			{
				tableFix.selectCell(tableFix.cell("The Cost of Living"));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// reselect the edited book
			{
				tableFix.selectCell(tableFix.cell("Adam Eva"));
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromGUIWithFiltering() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			final JTableFixture tableFix = frameFix.table("table");
			final JTextComponentFixture filterTextBox = frameFix
					.textBox("filterText");
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// Filter some books
			{
				filterTextBox.enterText("The");
				pause(200);
			}

			// select one book
			{
				tableFix.selectCell(tableFix.cell("The Cost of Living"));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// select another book
			{
				tableFix.selectCell(tableFix
						.cell("The Humble Administrator's Garden"));
				pause(200);

				titleTextBox.requireText("The Humble Administrator's Garden");
				yearTextBox.requireText("1985");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("The Humble Administrator's Garden", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1985, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// edit selected title
			{
				titleTextBox.setText("Adam Eva");
				yearTextBox.focus();
				pause(200);

				yearTextBox.requireText("1985");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1985, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// edit selected year
			{
				yearTextBox.setText("1000");
				titleTextBox.focus();
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// reselect the first book
			{
				tableFix.selectCell(tableFix.cell("The Cost of Living"));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// reselect the edited book after ceaning the filter
			{
				filterTextBox.deleteText();
				pause(200);
				tableFix.selectCell(tableFix.cell("Adam Eva"));
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testEditFromModel() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// Select a book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(5));
				pause(200);

				titleTextBox.requireText("Miguel Street");
				yearTextBox.requireText("1959");
				authorTextBox.requireText("V.S. Naipaul");
				assertEquals("Miguel Street", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1959, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("V.S. Naipaul", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// Select another book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(10));
				pause(200);

				titleTextBox.requireText("Such a Long Journey");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Such a Long Journey", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// Edit the title of the selected book
			{
				sc.getModel().getSelectedBook().setTitle("Another nice title");
				pause(200);

				titleTextBox.requireText("Another nice title");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Another nice title", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// Edit the author of the selected book
			{
				sc.getModel().getSelectedBook().getAuthor().setName(
						"Another nice name");
				pause(200);

				titleTextBox.requireText("Another nice title");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Another nice name");
				assertEquals("Another nice title", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Another nice name", sc.getModel()
						.getSelectedBook().getAuthor().getName());
			}

			// reselect the first book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(5));
				pause(200);

				titleTextBox.requireText("Miguel Street");
				yearTextBox.requireText("1959");
				authorTextBox.requireText("V.S. Naipaul");
				assertEquals("Miguel Street", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1959, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("V.S. Naipaul", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// reselect the edited book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(10));
				pause(200);

				titleTextBox.requireText("Another nice title");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Another nice name");
				assertEquals("Another nice title", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Another nice name", sc.getModel()
						.getSelectedBook().getAuthor().getName());
			}
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testEditFromModelWithSorting() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			final JTableFixture tableFix = frameFix.table("table");
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// select by title
			{
				tableFix.tableHeader().clickColumn(0);
				pause(200);
			}

			// Select a book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(5));
				pause(200);

				titleTextBox.requireText("Miguel Street");
				yearTextBox.requireText("1959");
				authorTextBox.requireText("V.S. Naipaul");
				assertEquals("Miguel Street", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1959, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("V.S. Naipaul", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// Select another book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(10));
				pause(200);

				titleTextBox.requireText("Such a Long Journey");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Such a Long Journey", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// Edit the title of the selected book
			{
				sc.getModel().getSelectedBook().setTitle("Another nice title");
				pause(200);

				titleTextBox.requireText("Another nice title");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Rohinton Mistry");
				assertEquals("Another nice title", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Rohinton Mistry", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// Edit the author of the selected book
			{
				sc.getModel().getSelectedBook().getAuthor().setName(
						"Another nice name");
				pause(200);

				titleTextBox.requireText("Another nice title");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Another nice name");
				assertEquals("Another nice title", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Another nice name", sc.getModel()
						.getSelectedBook().getAuthor().getName());
			}

			// reselect the first book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(5));
				pause(200);

				titleTextBox.requireText("Miguel Street");
				yearTextBox.requireText("1959");
				authorTextBox.requireText("V.S. Naipaul");
				assertEquals("Miguel Street", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1959, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("V.S. Naipaul", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

			// reselect the edited book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(10));
				pause(200);

				titleTextBox.requireText("Another nice title");
				yearTextBox.requireText("1991");
				authorTextBox.requireText("Another nice name");
				assertEquals("Another nice title", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1991, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Another nice name", sc.getModel()
						.getSelectedBook().getAuthor().getName());
			}
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testEditFromModelWithFiltering() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			final JTextComponentFixture filterTextBox = frameFix
					.textBox("filterText");
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// Filter some books
			{
				filterTextBox.enterText("The");
				pause(200);
			}

			// select one book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(12));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// select another book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(16));
				pause(200);

				titleTextBox.requireText("The Humble Administrator's Garden");
				yearTextBox.requireText("1985");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("The Humble Administrator's Garden", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1985, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// edit selected title
			{
				titleTextBox.setText("Adam Eva");
				yearTextBox.focus();
				pause(200);

				yearTextBox.requireText("1985");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1985, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// edit selected year
			{
				yearTextBox.setText("1000");
				titleTextBox.focus();
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());

			}
			// reselect the first book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(12));
				pause(200);

				titleTextBox.requireText("The Cost of Living");
				yearTextBox.requireText("1999");
				authorTextBox.requireText("Arunhati Roy");
				assertEquals("The Cost of Living", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1999, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Arunhati Roy", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
			// reselect the edited book after ceaning the filter
			{
				filterTextBox.deleteText();
				pause(200);
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(16));
				pause(200);

				titleTextBox.requireText("Adam Eva");
				yearTextBox.requireText("1000");
				authorTextBox.requireText("Vikram Seth");
				assertEquals("Adam Eva", sc.getModel().getSelectedBook()
						.getTitle());
				assertEquals(1000, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("Vikram Seth", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectionOfFilteredBook() {
		MasterDetailTablePanel panel = new MasterDetailTablePanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			MasterDetailTableShowcase sc = new MasterDetailTableShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			final JTextComponentFixture filterTextBox = frameFix
					.textBox("filterText");
			final JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			final JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			final JTextComponentFixture authorTextBox = frameFix
					.textBox("authorTextField");

			// Filter some books
			{
				filterTextBox.enterText("The");
				pause(200);
			}

			// select one book
			{
				sc.getModel().setSelectedBook(sc.getModel().getBooks().get(3));
				pause(200);

				titleTextBox.requireText("A Bend in the River");
				yearTextBox.requireText("1979");
				authorTextBox.requireText("V.S. Naipaul");
				assertEquals("A Bend in the River", sc.getModel()
						.getSelectedBook().getTitle());
				assertEquals(1979, sc.getModel().getSelectedBook().getYear()
						.intValue());
				assertEquals("V.S. Naipaul", sc.getModel().getSelectedBook()
						.getAuthor().getName());
			}
		} finally {
			frameFix.cleanUp();
		}

	}

	@SuppressWarnings("unused")
	private void buildContent(MasterDetailTableShowcase sc) {
		StringBuilder sb = new StringBuilder();
		CobeanTestUtil.buildStringArray(sb, sc.getModel().getBooks(), "title",
				"year", "author.name");
		System.out.println(sb.toString());
	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
